import useMainStore from '@/stores/modules/main'
import axios from 'axios'

import { BASE_URL, TIMEOUT } from './config'

const mainStore = useMainStore()

class MyRequest {
  constructor(baseURL, timeout=10000) {
    this.instance = axios.create({
      baseURL,
      timeout
    })
    // 添加请求拦截器
    this.instance.interceptors.request.use((config) => {
      mainStore.isLoading = true
      return config;
    }, (error) => {
      return Promise.reject(error);
    })
    // 添加响应拦截器
    this.instance.interceptors.response.use((response) => {
      mainStore.isLoading = false
      return response;
    }, (error) => {
      mainStore.isLoading = false
      return Promise.reject(error);
    })
  }

  request(config) {
    return new Promise((resolve, reject) => {
      this.instance.request(config).then(res => {
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // get(config) {
  //   return this.request({ ...config, method: "get" })
  // }

  // post(config) {
  //   return this.request({ ...config, method: "post" })
  // }
}

export default new MyRequest(BASE_URL, TIMEOUT)


